package com.hy.main.demo.io;

import org.junit.Test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Author yang DongYong
 * @Date 2022/4/26 - 04 - 26 - 21:19
 * @Description: com.hy.main.demo.file
 * @version: 1.0
 */
public class BufferWriters {

    @Test
    public void bufferWritDemo () {
        String path = "e:\\ok.txt";
        BufferedWriter bufferedWriter = null;
        try {
             // BufferedWriter(new FileWriter(path))如果FileWriter构造器里面第二个参数没有true则为覆盖模式，有则为追加模式
             bufferedWriter = new BufferedWriter(new FileWriter(path));
             bufferedWriter.write("周一");
             // 插入一个换行符
             bufferedWriter.newLine();
             bufferedWriter.write("周二");
             bufferedWriter.newLine();
             bufferedWriter.write("周三");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 这里只需要关闭BufferedWriter的流即可，因为它底层会帮我们关闭FileWriter的流
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
